<?php
/*
 * MiniMe - a minimalistic OStatus complient microblogging tool
 * Copyright (C) 2010, Tuomas Koski - koski.tuomas@gmail.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @author     Tuomas Koski <koski.tuomas@gmail.com>
 * @copyright  Tuomas Koski
 * @license    http://www.gnu.org/licenses/lgpl.txt
 */

require_once 'vfsStream/vfsStream.php';
$basedir = dirname(__FILE__);

require_once $basedir . '/../../../../../classes/minime/pubsubhubbub/subscription/loader.php';
require_once $basedir . '/../../../../../classes/minime/pubsubhubbub/subscription.php';
require_once $basedir . '/../../../../../classes/minime/pubsubhubbub/minimesubscribers.php';
require_once $basedir . '/../../../../../classes/minime/pubsubhubbub/minimesubscriber.php';

class Minime_Pubsubhubbub_Subscription_LoaderTest extends PHPUnit_Framework_TestCase {

    public function testSuccessLoadOfSubscription() {
        $content = '<?xml version="1.0" encoding="UTF-8"?>
<pubsubhubbub_subscription id="8266d1b6663888a527d0a70aa0eca4b7">
  <callback>http://callback.com/callback.php</callback>
  <topic>http://identi.ca/api/statuses/user_timeline/100.atom</topic>
  <lease_seconds>601</lease_seconds>
  <secret>SECRETQSDF345SDRT35ERG35SDFG35SRG1</secret>
  <verify_token>VERFIYTOKENSGFSDFSDR345EGSGDFGDFGDFGDF1</verify_token>
  <title>This is the topic of the followed node.1</title>
  <subid>8266d1b6663888a527d0a70aa0eca4b7</subid>
  <hub>http://www.super.com/hub</hub>
  <subscribers>
    <subscriber>tkoski</subscriber>
    <subscriber>cindy</subscriber>
    <subscriber>pamelaa</subscriber>
  </subscribers>
</pubsubhubbub_subscription>
';
        
        # Register our virtual file system
        vfsStreamWrapper::register();
        
        # Create a new root directory
        $root = new vfsStreamDirectory('srv');
        $root->addChild(new vfsStreamDirectory('datastorage'));
        $root->getChild('datastorage')->addChild(new vfsStreamDirectory('8266d1b6663888a527d0a70aa0eca4b7'));
        $root->getChild('datastorage')->getChild('8266d1b6663888a527d0a70aa0eca4b7')->addChild(vfsStream::newFile('subscription.xml')->withContent($content));
        vfsStreamWrapper::setRoot($root);
       
        $conf['datastore']['pubsubhubbub_to'] = vfsStream::url('srv/datastorage');
        
        $loader = new Minime_pubsubhubbub_Subscription_Loader($conf);
        $this->assertEquals($content, $loader->getSubscription("8266d1b6663888a527d0a70aa0eca4b7")->asXML());
    }
    
    public function testLoadOfSubscriptionWhereSubscriptionDoesNotExist() {
  
        # Register our virtual file system
        vfsStreamWrapper::register();
        
        # Create a new root directory
        $root = new vfsStreamDirectory('srv');
        $root->addChild(new vfsStreamDirectory('datastorage'));
        $root->getChild('datastorage')->addChild(new vfsStreamDirectory('8266d1b6663888a527d0a70aa0eca4b7'));
        $root->getChild('datastorage')->getChild('8266d1b6663888a527d0a70aa0eca4b7')->addChild(vfsStream::newFile('subscription.xml')->withContent(""));
        vfsStreamWrapper::setRoot($root);
       
        $conf['datastore']['pubsubhubbub_to'] = vfsStream::url('srv/datastorage');
        
        $loader = new Minime_pubsubhubbub_Subscription_Loader($conf);
        $subs = $loader->getSubscription("doesnotexists");
        $this->assertEquals(false, $subs);
    }
    
}

